package com.eling.elcms.travel.service.impl;

import com.eling.elcms.core.service.impl.GenericManagerImpl;
import com.eling.elcms.travel.dao.ICheckOutSetlConfirmDao;
import com.eling.elcms.travel.model.CheckOutDeal;
import com.eling.elcms.travel.model.CheckOutDeal.Status;
import com.eling.elcms.travel.model.CheckOutSetlConfirm;
import com.eling.elcms.travel.service.ICheckOutDealManager;
import com.eling.elcms.travel.service.ICheckOutSetlConfirmManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CheckOutSetlConfirmManagerImpl extends GenericManagerImpl<CheckOutSetlConfirm, Long> implements ICheckOutSetlConfirmManager{

	ICheckOutSetlConfirmDao checkOutSetlConfDao;
	
	@Autowired
	private ICheckOutDealManager checkOutDealManager;

	@Autowired
	public void setCheckOutSetlConfirmDao(ICheckOutSetlConfirmDao checkOutSetlConfDao) {
		this.dao = checkOutSetlConfDao;
		this.checkOutSetlConfDao = checkOutSetlConfDao;
	}

	@Override
	public CheckOutSetlConfirm saveConfirm(CheckOutSetlConfirm cond) {
		
		cond = save(cond);
		
		//回写结算单状态
		CheckOutDeal checkOutDeal = checkOutDealManager.get(cond.getCheckOutDeal().getPkCheckOutDeal());
		checkOutDeal.setStatus(Status.Confirm);
		checkOutDealManager.save(checkOutDeal);
		return cond;
	}

	@Override
	public CheckOutDeal changeOnCredit(CheckOutDeal cond) {
		CheckOutDeal checkOutDeal = new CheckOutDeal();
		checkOutDeal.setPkCheckOutDeal(cond.getPkCheckOutDeal());
		List<CheckOutDeal> checkOutDeals = checkOutDealManager.query(checkOutDeal);
		if(checkOutDeals!=null && !checkOutDeals.isEmpty()){
			checkOutDeal = checkOutDeals.get(0);
		}
		checkOutDeal.setIsSales(cond.getIsSales());
		checkOutDeal.setMoney(cond.getMoney());
		return checkOutDealManager.save(checkOutDeal);
	}

}
